package com.ithanwen.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ithanwen.mapper.EmpMapper;
import com.ithanwen.mapper.EmpExprMapper;
import com.ithanwen.pojo.*;
import com.ithanwen.service.EmpLogService;
import com.ithanwen.service.EmpService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created By tony
 * <br/>Date: 2025/7/10 15:00
 * <br/>Description:
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        Page<Emp> empPage = (Page<Emp>)empMapper.search(empQueryParam.getName(), empQueryParam.getGender(), empQueryParam.getBegin(), empQueryParam.getEnd());
        return new PageResult<Emp>(empPage.getTotal(), empPage.getResult());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        try {
            emp.setUpdateTime(LocalDateTime.now());
            emp.setCreateTime(LocalDateTime.now());
            empMapper.insert(emp);

            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)) {
                exprList.forEach(expr -> {
                    expr.setEmpId(emp.getId());
                });
                empExprMapper.insertBatch(exprList);
            }

        } finally {
            EmpLog emplog = new EmpLog(null,LocalDateTime.now(),"新增员工：" + emp);
            empLogService.insertLog(emplog);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {;
        //直接更新员工表的基本信息
        empMapper.updateById(emp);
        //删除工作经历表的记录
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //重新添加工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(exprList); // 批量更新
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Emp findById(Integer id) {
        //查询员工基本信息
        Emp emp = empMapper.selectById(id);
        //查询就职信息
        List<EmpExpr> exprList = empExprMapper.selectByEmpId(id);
        emp.setExprList(exprList);
        return emp;
    }

    @Override
    public List<Emp> findall() {
        return empMapper.selectList(null);
    }


}
